package com.googlecode.maratische.google;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import be.lechtitseb.google.reader.api.core.GoogleReader;

import com.googlecode.maratische.google.dao.CommandDao;
import com.googlecode.maratische.google.dao.FeedDao;
import com.googlecode.maratische.google.dao.ItemDao;
import com.googlecode.maratische.google.dao.LabelDao;
import com.googlecode.maratische.google.dao.PropertyDao;
import com.googlecode.maratische.google.dao.SpamDao;
import com.googlecode.maratische.google.dao.impl.CommandDaoImpl;
import com.googlecode.maratische.google.dao.impl.FeedDaoImpl;
import com.googlecode.maratische.google.dao.impl.ItemDaoImpl;
import com.googlecode.maratische.google.dao.impl.LabelDaoImpl;
import com.googlecode.maratische.google.dao.impl.PropertyDaoImpl;
import com.googlecode.maratische.google.dao.impl.SpamDaoImpl;
import com.googlecode.maratische.google.model.Constants;

public class DaoAction {
	private static DaoAction daoAction = null;;
	
	private FeedDao feedDao;
	private ItemDao itemDao;
	private CommandDao commandDao;
	private PropertyDao propertyDao;
	private LabelDao labelDao;
	private SpamDao spamDao;
	private Connection connection;
	private GoogleReader googleReader = null;

	public static void createDaoAction() throws ClassNotFoundException, SQLException {
		String userHome = null;
		if (userHome == null) {
			userHome = System.getProperty("user.home");
			userHome += File.separator + Constants.RSS_READER_FOLDER_NAME;
		}
		userHome += File.separator;
		final DBConnect connect = new DBConnect();
		connect.init(userHome);
		daoAction = new DaoAction(connect.getConnection());
	}
	public static DaoAction getDaoAction() {
		return daoAction;
	}
	
	private DaoAction(Connection connection) {
		this.connection = connection;
	}
	
	public GoogleReader getGoogleReader() throws BaseException {
		if (googleReader == null || !googleReader.isAuthenticated()) {
			googleReader = MainController.getMainController().login();
		}
		return googleReader;
	}
	public void setGoogleReader(GoogleReader googleReader) throws BaseException {
		this.googleReader = googleReader;
	}

	public PropertyDao getPropertyDao() {
		if (propertyDao == null) {
			propertyDao = new PropertyDaoImpl(this.connection);
		}
		return propertyDao;
	}
	
	public FeedDao getFeedDao() {
		if (feedDao == null) {
			feedDao = new FeedDaoImpl(connection, getItemDao());
		}
		return feedDao;
	}
	
	public LabelDao getLabelDao() {
		if (labelDao == null) {
			labelDao = new LabelDaoImpl(connection);
		}
		return labelDao;
	}
	
	public ItemDao getItemDao() {
		if (itemDao == null) {
			itemDao = new ItemDaoImpl(connection);
		}
		return itemDao;
	}
	
	public CommandDao getCommandDao() {
		if (commandDao == null) {
			commandDao = new CommandDaoImpl(connection);
		}
		return commandDao;
	}
	
	public SpamDao getSpamDao() {
		if (spamDao == null) {
			spamDao = new SpamDaoImpl(connection);
		}
		return spamDao;
	}
}
